/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Lixo;

import bancodedados3d.*;
import com.sun.j3d.utils.behaviors.mouse.MouseRotate;
import com.sun.j3d.utils.geometry.ColorCube;
import com.sun.j3d.utils.geometry.Sphere;
import com.sun.j3d.utils.geometry.Text2D;
import com.sun.j3d.utils.universe.SimpleUniverse;
import java.awt.BorderLayout;
import java.awt.Font;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.DirectionalLight;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.swing.JPanel;
import javax.vecmath.Color3f;
import javax.vecmath.Vector3d;
import javax.vecmath.Vector3f;
import com.sun.j3d.utils.*;
import com.sun.j3d.utils.behaviors.mouse.MouseWheelZoom;
import com.sun.j3d.utils.geometry.Primitive;
import javax.media.j3d.OrientedShape3D;
import javax.media.j3d.Shape3D;

/**
 *
 *
 * Title: Wrap3D
 * Description: this holds a j3d rendered image in a Swing container.
 *              Now you can plug this into anything that takes Swing objects
 *
 * Copyright:    Copyright (c) 2001
 * Company:      Meissner Software Development, LLC
 * @author Karl Meissner
 * @version 1.0
 */
// make a scene with a cube and a label
public class JPanel3D extends JPanel {
        private SimpleUniverse simpleU = new SimpleUniverse();



    BranchGroup createSceneGraph(int scene_type) {

        BranchGroup objRoot = new BranchGroup();
        TransformGroup root_group = new TransformGroup();
        root_group.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); // allow the mouse behavior to rotate the scene
        root_group.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);

        objRoot.addChild(root_group);  // this is the local origin  - everyone hangs off this - moving this move every one

        switch (scene_type) // decide what to draw
        {
            case 0:
                root_group.addChild(new ColorCube(0.2));  // add a color cube
                root_group.addChild(new Texto3D(0.2f, 0.2f, 0.0f, "ColorCube")); // add a label to the scene
                break;
            case 1:
                Transform3D sphere_transform = new Transform3D();
                sphere_transform.setScale(0.2);       // shrink the sphere so our view point is not inside it
                TransformGroup sphere_group = new TransformGroup(sphere_transform);
                sphere_group.addChild(new Sphere());  // add a sphere
                root_group.addChild(sphere_group);

                root_group.addChild(new Texto3D(0.2f, 0.2f, 0.0f, "sphere")); // add a label to the scene

                BoundingSphere bounds = new BoundingSphere(); // the render will only light objects inside this volume
                bounds.setRadius(1000.0);

                // add a light so we can see the sphere
                DirectionalLight lightD = new DirectionalLight();
                lightD.setInfluencingBounds(bounds);
                lightD.setDirection(new Vector3f(.7f, -.7f, -.7f));  // point the light right, down, into the screen
                objRoot.addChild(lightD);
                break;
            case 2:
                Transform3D painelTrans = new Transform3D();
                painelTrans.setScale(0.2);       // shrink the sphere so our view point is not inside it
                TransformGroup painelGrup = new TransformGroup(painelTrans);
                root_group.addChild(painelGrup);

                root_group.addChild(new Texto3D(0.2f, 0.2f, 0.0f, "Teste")); // add a label to the scene

                BoundingSphere area = new BoundingSphere(); // the render will only light objects inside this volume
                area.setRadius(1000.0);

                // add a light so we can see the sphere
                DirectionalLight luz = new DirectionalLight();
                luz.setInfluencingBounds(area);
                luz.setDirection(new Vector3f(.7f, -.7f, -.7f));  // point the light right, down, into the screen
                objRoot.addChild(luz);
                break;
            case 3:
                Transform3D resultset3D = new Transform3D();
                resultset3D.setScale(0.2);
                TransformGroup resultsetGrup = new TransformGroup(resultset3D);

                resultsetGrup.addChild(new OrientedShape3D());  // add a sphere
                root_group.addChild(resultsetGrup);

                root_group.addChild(new Texto3D(0.2f, 0.2f, 0.0f, "Teste")); // add a label to the scene

                BoundingSphere area1 = new BoundingSphere(); // the render will only light objects inside this volume
                area1.setRadius(1000.0);

                // add a light so we can see the sphere
                DirectionalLight luz1 = new DirectionalLight();
                luz1.setInfluencingBounds(area1);
                luz1.setDirection(new Vector3f(.7f, -.7f, -.7f));  // point the light right, down, into the screen
                objRoot.addChild(luz1);


                break;
        }


        // another J3D bug with JInternalFrames - if there are a lot of Text2D obects and several scenes,
        // there is a strange interaction
        // between selecting windows and texture memory.  After selecting several
        // JInternalFrames with Canvas3D items, Text2D items start
        // drawing as opaque white rectangles.
        // the only work around I can find is to either only have a single scene or
        // not to use Text2D
        // I would appriciate any other workarounds for JInternalFrames people
        // can come up with - meissner-sd@ieee.org
        // reproduce bug HERE
/*
        int Num_Text_Msg;
        Num_Text_Msg=20;

        int h;
        for ( h = 0; h<Num_Text_Msg; ++h) {
        Transform3D msg_trans = new Transform3D();
        msg_trans.setTranslation( new Vector3d( Math.random() - 0.5, Math.random() - 0.5, Math.random() - 0.5 ) );

        TransformGroup msg_group = new TransformGroup( msg_trans );
        Text2D center_msg  = new Text2D( "text" + h,
        new Color3f( 1.0f, 1.0f, 1.0f),
        "Helvetica", 24, Font.PLAIN );
        msg_group.addChild( center_msg );
        objRoot.addChild( msg_group );
        }
         */

        MouseWheelZoom zoom = new MouseWheelZoom(root_group);
        zoom.setSchedulingBounds(new BoundingSphere());
        objRoot.addChild(zoom);

        MouseRotate mouseRotate = new MouseRotate(root_group);  // add the mouse behavior
        mouseRotate.setSchedulingBounds(new BoundingSphere());
        objRoot.addChild(mouseRotate);
        return objRoot;
    }

    public JPanel3D(String[][] str) {
        // construct the 3D image
//        Canvas3D canvas3D = new Canvas3D(null);
//        SimpleUniverse simpleU = new SimpleUniverse();
//        BranchGroup scene = createSceneGraph(scene_type);

        tabela3D tabela = new tabela3D(str);

        simpleU.getViewingPlatform().setNominalViewingTransform();       // This will move the ViewPlatform back a bit so the
        simpleU.addBranchGraph(tabela);


        Canvas3D canvas3D = simpleU.getCanvas();
        canvas3D.setBounds(0, 0, 500, 250);


        this.setLayout(new BorderLayout());
        this.setOpaque(false);
        this.add("Center", canvas3D);   // <-- HERE IT IS - tada! j3d in swing
//        this.add(canvas3D);
//        this.size();

    }

    public JPanel3D() {
        // construct the 3D image
//        Canvas3D canvas3D = new Canvas3D(null);
//        SimpleUniverse simpleU = new SimpleUniverse();
//        BranchGroup scene = createSceneGraph(scene_type);

//        tabela3D tabela = new tabela3D(str);

        simpleU.getViewingPlatform().setNominalViewingTransform();       // This will move the ViewPlatform back a bit so the
//        simpleU.addBranchGraph(tabela);


        Canvas3D canvas3D = simpleU.getCanvas();


        System.out.println(JPanel3D.WIDTH);
        System.out.println(this.getWidth());

        canvas3D.setBounds(0, 0, 500, 250);



        this.setLayout(new BorderLayout());
        this.setOpaque(false);
        this.add("Center", canvas3D);   // <-- HERE IT IS - tada! j3d in swing
//        this.add(canvas3D);
//        this.size();

    }

    public JPanel3D(int scene_type) {
        // construct the 3D image
//        Canvas3D canvas3D = new Canvas3D(null);
//        SimpleUniverse simpleU = new SimpleUniverse();
        BranchGroup scene = createSceneGraph(scene_type);

//        tabela3D tabela = new tabela3D(str);

        simpleU.getViewingPlatform().setNominalViewingTransform();       // This will move the ViewPlatform back a bit so the
        simpleU.addBranchGraph(scene);


        Canvas3D canvas3D = simpleU.getCanvas();


        System.out.println(JPanel3D.WIDTH);
        System.out.println(this.getWidth());

        canvas3D.setBounds(0, 0, 500, 250);



        this.setLayout(new BorderLayout());
        this.setOpaque(false);
        this.add("Center", canvas3D);   // <-- HERE IT IS - tada! j3d in swing
//        this.add(canvas3D);
//        this.size();

    }

    static public void main(String[][] args) {
//        new JPanel3D(0);
//        new JPanel3D(1);
        new JPanel3D(args);
    }

    /**
     * @return the simpleU
     */
    public SimpleUniverse getSimpleU() {
        return simpleU;
    }

    /**
     * @param simpleU the simpleU to set
     */
    public void setSimpleU(SimpleUniverse simpleU) {
        this.simpleU = simpleU;
    }
}
